package com.xvdong.gadget;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.text.DecimalFormat;

import javax.imageio.ImageIO;

/**
 * 对比敏感度测试图片生成
 */
public class CSFChartGenerator {

    public static void main(String[] args) {
        generateContrastFrequencySeries("csf_output", 300, 300);
    }

    public static void generateContrastFrequencySeries(String outputDirName, int width, int height) {
        // 9个对比度等级（按照对数间隔）
        double[] contrasts = {
                0.35, 0.23, 0.14, 0.09, 0.06, 0.036, 0.023, 0.014, 0.009
        };
        int[] frequencies = {3, 6, 12, 18, 24};  // 5个空间频率

        File outputDir = new File(outputDirName);
        if (!outputDir.exists()) outputDir.mkdirs();

        for (int freq : frequencies) {
            for (int i = 0; i < contrasts.length; i++) {
                double contrast = contrasts[i];
                String contrastStr = new DecimalFormat("0.###").format(contrast);
                String filename = String.format("csf_freq_%02d_contrast_%d_%s.png", freq, i + 1, contrastStr.replace(".", "_"));
                File file = new File(outputDir, filename);

                generateSingleChart(file.getAbsolutePath(), width, height, contrast, freq);
                System.out.println("已保存: " + file.getAbsolutePath() +
                        " (freq = " + freq + ", contrast = " + contrastStr + ")");
            }
        }
    }

    public static void generateSingleChart(String outputPath, int width, int height, double contrast, int frequency) {
        int scale = 4; // 超采样倍数
        int bigW = width * scale;
        int bigH = height * scale;

        BufferedImage bigImage = new BufferedImage(bigW, bigH, BufferedImage.TYPE_INT_ARGB);

        // 先生成大图
        for (int x = 0; x < bigW; x++) {
            for (int y = 0; y < bigH; y++) {
                int dx = x - bigW / 2;
                int dy = y - bigH / 2;
                double distance = Math.sqrt(dx * dx + dy * dy);
                int radius = Math.min(bigW, bigH) / 2 - scale * 10;

                if (distance <= radius) {
                    double sine = Math.sin(2 * Math.PI * frequency * dx / (double)(2 * radius));
                    double value = 128 + (128 * contrast * sine);
                    int gray = (int) Math.max(0, Math.min(255, value));
                    int rgb = new Color(gray, gray, gray, 255).getRGB();
                    bigImage.setRGB(x, y, rgb);
                } else {
                    bigImage.setRGB(x, y, 0x00000000);
                }
            }
        }

        // 缩放到目标大小并开启高质量插值
        BufferedImage finalImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = finalImage.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.drawImage(bigImage, 0, 0, width, height, null);
        g2.dispose();

        try {
            ImageIO.write(finalImage, "png", new File(outputPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
